#include "MainWindow.h"
#include "CentralWidget.h"
#include "CommandFactory.h"
#include <QDebug>
#include <QKeyEvent>
#include <QMenuBar>
#include <memory>

using namespace std;

MainWindow::MainWindow(QWidget* parent)
 	: QMainWindow(parent)
{
	setCentralWidget(new CentralWidget);
	createMenus();
	setGeometry(100, 100, 800, 600);
	initialize();
}


MainWindow::~MainWindow() {}

void MainWindow::initialize()
{
}

void MainWindow::createMenus()
{
	createFileMenu();
	createEditMenu();
	createHelpMenu();
}

void MainWindow::createFileMenu()
{
	QMenu* menu = menuBar()->addMenu(tr("&File"));
	QAction* action = menu->addAction("E&xit");
	connect(action, SIGNAL(triggered()), this, SLOT(onExit()));
}

void MainWindow::onExit()
{
	shared_ptr<ICommand> command = CommandFactory::createMainWindowCommand(MAIN_EXIT, this);
	command->execute();
}

void MainWindow::createEditMenu()
{

}

void MainWindow::createHelpMenu()
{
	QMenu* menu = menuBar()->addMenu("&Help");
	QAction* action = menu->addAction("&About");
	connect(action, SIGNAL(triggered()), this, SLOT(onAbout()));
}

void MainWindow::onAbout()
{
	//CommandFactory::createMainWindowCommand(ABOUT);
}

void MainWindow::keyPressEvent(QKeyEvent* e)
{
	int key = e->key();
	if (key >= Qt::Key_0 && key <= Qt::Key_9 || key >= Qt::Key_A && key <= Qt::Key_Z)
	{
		CentralWidget* center = static_cast<CentralWidget*>(centralWidget());
		center->showStockDialog(key);
	}
}
